//
//  JMSina.m
//  JMLibrary
//
//  Created by zhengxianda on 2018/7/24.
//

#import "JMSina.h"

@implementation JMSina

- (void)registerApp:(NSString *)appId{
    [WeiboSDK registerApp:appId];
}

- (BOOL)handleOpenURL:(NSURL *)url{
    return [WeiboSDK handleOpenURL:url delegate:self];
}

- (void)shareToChannel:(JMShareChannel)channel messageObject:(JMShareMessageObject *)messageObject handler:(JMShareRequestHandler)handler{
    [super shareToChannel:channel messageObject:messageObject handler:handler];
    
    WBMessageObject * object = [WBMessageObject message];
    
    if (messageObject.musicDataURL && messageObject.musicPageURL) {
        WBWebpageObject *webpageObject = [WBWebpageObject object];
        webpageObject.objectID = [messageObject.musicPageURL absoluteString];
        webpageObject.title = messageObject.title;
        webpageObject.description = messageObject.descriptions;
        webpageObject.thumbnailData = messageObject.thumbImage;
        webpageObject.webpageUrl = [messageObject.musicPageURL absoluteString];
        object.mediaObject = webpageObject;
    }else if (messageObject.webPageURL) {
        WBWebpageObject *webpageObject = [WBWebpageObject object];
        webpageObject.objectID = [messageObject.webPageURL absoluteString];
        webpageObject.title = messageObject.title;
        webpageObject.description = messageObject.descriptions;
        webpageObject.thumbnailData = messageObject.thumbImage; 
        webpageObject.webpageUrl = [messageObject.webPageURL absoluteString];
        object.mediaObject = webpageObject;
    }else if (messageObject.shareImage) {
        WBImageObject * imageObject = [WBImageObject object];
        imageObject.imageData = messageObject.shareImage;
        object.imageObject = imageObject;
    }else{
        object.text = messageObject.text;
    }
    
    WBSendMessageToWeiboRequest * request = [WBSendMessageToWeiboRequest requestWithMessage:object];
    
    BOOL result = [WeiboSDK sendRequest:request];
    if (!result) {
        [self callHandlerWithCode:WeiboSDKResponseStatusCodeSentFail];
    }
}

- (NSString *)errorMsgWithCode:(int)code{
    NSString *msg = @"";
    switch (code) {
        case WeiboSDKResponseStatusCodeSuccess:
            msg = @"成功";
            break;
        case WeiboSDKResponseStatusCodeUserCancel:
            msg = @"用户取消发送";
            break;
        case WeiboSDKResponseStatusCodeSentFail:
            msg = @"发送失败";
            break;
        case WeiboSDKResponseStatusCodeAuthDeny:
            msg = @"授权失败";
            break;
        case WeiboSDKResponseStatusCodeUserCancelInstall:
            msg = @"用户取消安装微博客户端";
            break;
        case WeiboSDKResponseStatusCodePayFail:
            msg = @"支付失败";
            break;
        case WeiboSDKResponseStatusCodeShareInSDKFailed:
            msg = @"分享失败 详情见response UserInfo";
            break;
        case WeiboSDKResponseStatusCodeUnsupport:
            msg = @"不支持的请求";
            break;
        case WeiboSDKResponseStatusCodeUnknown:
            msg = @"";
            break;
    }
    return msg;
}

- (void)didReceiveWeiboRequest:(WBBaseRequest *)request {
    
}

- (void)didReceiveWeiboResponse:(WBBaseResponse *)response {
    [self callHandlerWithCode:response.statusCode];
}

@end
